package com.yanqu.road.server.gameplayer.module.activity.newserverwelfare;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newserverwelfare.NewServerWelfareReward;

import com.yanqu.road.entity.enums.activity.eNewServerWelfareActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.LogNewServerWelfareGerReward;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserOnlineTime;
import com.yanqu.road.logic.bussiness.activity.NewServerWelfareBusiness;
import com.yanqu.road.pb.activity.NewServerWelfareProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.newserverwelfare.NewServerWelfareMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.NewServerWelfarePb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Date;
import java.util.Map;

public class NewServerWelfareModule extends GeneralModule {

    public NewServerWelfareModule(GamePlayer player) {
        super(player);
    }


    private UserOnlineTime userOnlineTime;


    @Override
    public boolean loadData() {
        ActivityInfo timeActivityInfo = NewServerWelfareMgr.getActivityInfo(eNewServerWelfareActivityType.OnlineTime.getValue());
        if (ActivityMgr.activityInShowTime(timeActivityInfo)) {
            userOnlineTime = NewServerWelfareBusiness.getUserOnlineTime(timeActivityInfo.getActivityId(), player.getUserId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userOnlineTime != null) {
            if (userOnlineTime.isInsertOption()) {
                NewServerWelfareBusiness.addUserOnlineTime(userOnlineTime);
            } else if (userOnlineTime.isUpdateOption()) {
                NewServerWelfareBusiness.updateUserOnlineTime(userOnlineTime);
            }
        }
        return true;
    }

    public void syncAllData() {
        Map<Integer, ActivityInfo> activityInfoMap = NewServerWelfareMgr.getActivityInfoMap();
        boolean needSync = false;
        if (activityInfoMap.isEmpty()) {
            return;
        }
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInTime(activityInfo)) {
                needSync = true;
            }
        }
        //活动没开在线时长 只开了其他的
        if (this.userOnlineTime == null && needSync) {
            NewServerWelfareProto.SyncActivityDataRespMsg.Builder builder = NewServerWelfarePb.createSyncData(0, NewServerWelfareMgr.getRewardCopyMap());
            player.sendPacket(ClientProtocol.U_SYNC_NEW_SERVER_WELFARE_DATA, builder);
            return;
        }
        if (needSync){
            //正常更新发送
            updateOnlineTime();
            NewServerWelfareProto.SyncActivityDataRespMsg.Builder builder = NewServerWelfarePb.createSyncData(userOnlineTime.getOnlineTime(), NewServerWelfareMgr.getRewardCopyMap());
            player.sendPacket(ClientProtocol.U_SYNC_NEW_SERVER_WELFARE_DATA, builder);
        }
    }

    @Override
    public void afterLogin() {
        if (NewServerWelfareMgr.checkActivityOntime(eNewServerWelfareActivityType.OnlineTime.getValue())) {
            if (null == userOnlineTime) {
                initUserOnlineData();
            }
        }
        syncAllData();
    }

    private void initUserOnlineData() {
        if (userOnlineTime != null &&
                userOnlineTime.getActivityId()==NewServerWelfareMgr.getActivityInfo(eNewServerWelfareActivityType.OnlineTime.getValue()).getActivityId()) {
            return;
        }
        ActivityInfo activityInfo = NewServerWelfareMgr.getActivityInfoMap().get(eNewServerWelfareActivityType.OnlineTime.getValue());
        UserInfo userInfo = UserMgr.getUserInfo(player.getUserId());
        if (userInfo == null) {
            return;
        }
        UserOnlineTime tmpUserOnlineTime = new UserOnlineTime();
        tmpUserOnlineTime.setActivityId(activityInfo.getActivityId());
        tmpUserOnlineTime.setUserId(player.getUserId());
        long currentTimeSeconds = DateHelper.getCurrentSecond();
        tmpUserOnlineTime.setLastUpdateTime(currentTimeSeconds);
        if (!DateHelper.isSameDay(userInfo.getLastLoginTime(), currentTimeSeconds)) {
            tmpUserOnlineTime.setOnlineTime(currentTimeSeconds - (DateHelper.getTodayZeroTime()));
        } else {
            tmpUserOnlineTime.setOnlineTime(currentTimeSeconds - userInfo.getLastLoginTime());
        }

        tmpUserOnlineTime.setInsertOption();
        userOnlineTime = tmpUserOnlineTime;
    }

    public void updateOnlineTime() {
        if (!NewServerWelfareMgr.checkActivityOntime(eNewServerWelfareActivityType.OnlineTime.getValue())) {
            return;
        }
        if (userOnlineTime == null || userOnlineTime.getActivityId()!=NewServerWelfareMgr.getActivityInfo(eNewServerWelfareActivityType.OnlineTime.getValue()).getActivityId()) {
            initUserOnlineData();
        }
        long currentTimeSecond = System.currentTimeMillis() / 1000;         //记录当前秒数
        UserInfo userInfo = UserMgr.getUserInfo(player.getUserId());
        if (userInfo == null) {
            return;
        }
        long addTime;
        //查看今天是活动第几天
        int day = NewServerWelfareMgr.getActivityHadOpenDay();
        if (day <= 0) {
            return;
        }

        //登录第一次更新时长数据
        if (userInfo.getLastLoginTime() >= userOnlineTime.getLastUpdateTime()) {
            addTime = currentTimeSecond - userInfo.getLastLoginTime();
            userOnlineTime.setOnlineTime(userOnlineTime.getOnlineTime() + addTime);
            userOnlineTime.setLastUpdateTime(currentTimeSecond);
            player.getModule(ActivityModule.class).startChanges();
            player.notifyListener(eGamePlayerEventType.NewServerWelfareTime.getValue(), userOnlineTime.getOnlineTime());
            player.getModule(ActivityModule.class).finishChanges();
            return;
        }

        //普通更新
        addTime = currentTimeSecond - userOnlineTime.getLastUpdateTime();
        userOnlineTime.setOnlineTime(userOnlineTime.getOnlineTime() + addTime);
        userOnlineTime.setLastUpdateTime(currentTimeSecond);
        player.getModule(ActivityModule.class).startChanges();
        player.notifyListener(eGamePlayerEventType.NewServerWelfareTime.getValue(), userOnlineTime.getOnlineTime());
        player.getModule(ActivityModule.class).finishChanges();

    }


    public boolean checkAndGetReward(int smallType, BaseActivityCondition condition) {

        if (!NewServerWelfareMgr.checkActivityOntime(smallType)) {
            return false;
        }
        Map<Integer, Map<Integer, NewServerWelfareReward>> rewardMap = NewServerWelfareMgr.getRewardMap();
        synchronized (rewardMap) {
            if (smallType == eNewServerWelfareActivityType.OnlineTime.getValue()) {  //每日不限量
                return true;
            }
            NewServerWelfareReward serverWelfareReward = rewardMap.get(smallType).get(condition.getConditionId());
            if (serverWelfareReward.getRewardRemain() <= 0) {
                return false;
            } else {
                serverWelfareReward.setRewardRemain(serverWelfareReward.getRewardRemain() - 1);
                AutoLogMgr.add(new LogNewServerWelfareGerReward(player.getUserId(),NewServerWelfareMgr.getActivityInfo(smallType).getActivityId(),player.getUserInfo().getVipExp(),condition.getActivityConditionInfo().getParamList().get(1).intValue(),new Date(),new Date(player.getUserInfo().getRegisterTime()*1000),smallType));
                return true;
            }
        }
    }


    public UserOnlineTime getUserOnlineTime() {
        return userOnlineTime;
    }

    public void setUserOnlineTime(UserOnlineTime userOnlineTime) {
        this.userOnlineTime = userOnlineTime;
    }
}
